<template>
  <el-form
    ref="registerFormRef"
    :model="registerForm"
    :rules="registerRules as any"
    class="register-form"
    @submit.prevent="handleRegister"
  >
    <el-form-item prop="username">
      <el-input
        v-model="registerForm.username"
        placeholder="用户名"
        :size="compact ? 'default' : 'large'"
        prefix-icon="User"
        clearable
      />
    </el-form-item>
    <el-form-item prop="email">
      <el-input
        v-model="registerForm.email"
        placeholder="邮箱地址"
        :size="compact ? 'default' : 'large'"
        prefix-icon="Message"
        clearable
      />
    </el-form-item>
    <el-form-item prop="phone">
      <el-input
        v-model="registerForm.phone"
        placeholder="手机号码"
        :size="compact ? 'default' : 'large'"
        prefix-icon="Phone"
        clearable
        maxlength="11"
      />
    </el-form-item>
    <el-form-item prop="password">
      <el-input
        v-model="registerForm.password"
        type="password"
        placeholder="密码（至少6位）"
        :size="compact ? 'default' : 'large'"
        prefix-icon="Lock"
        show-password
        clearable
      />
    </el-form-item>
    <el-form-item prop="confirmPassword">
      <el-input
        v-model="registerForm.confirmPassword"
        type="password"
        placeholder="确认密码"
        :size="compact ? 'default' : 'large'"
        prefix-icon="Lock"
        show-password
        clearable
      />
    </el-form-item>
    <el-form-item>
      <el-checkbox v-model="registerForm.agreement"
        >我已阅读并同意《用户协议》和《隐私政策》</el-checkbox
      >
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        :size="compact ? 'default' : 'large'"
        :loading="authStore.isLoading"
        :disabled="!registerForm.agreement"
        class="register-button"
        @click="handleRegister"
        >{{ authStore.isLoading ? '注册中...' : '注册' }}</el-button
      >
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useAuthStore } from '@/stores/auth'
import type { RegisterRequest } from '@/types'

const props = defineProps<{ compact?: boolean }>()
const emit = defineEmits<{ (e: 'success'): void }>()

const authStore = useAuthStore()
const registerFormRef = ref()
const registerForm = ref<{
  username: string
  email: string
  phone: string
  password: string
  confirmPassword: string
  agreement: boolean
}>({
  username: '',
  email: '',
  phone: '',
  password: '',
  confirmPassword: '',
  agreement: false,
})

const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' },
    { pattern: /^[a-zA-Z0-9_]+$/, message: '只能包含字母、数字和下划线', trigger: 'blur' },
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' },
  ],
  phone: [
    { required: true, message: '请输入手机号码', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule: unknown, value: string, callback: (error?: Error) => void) => {
        if (value !== registerForm.value.password) callback(new Error('两次输入的密码不一致'))
        else callback()
      },
      trigger: 'blur',
    },
  ],
}

const handleRegister = async () => {
  if (!registerFormRef.value) return
  await registerFormRef.value.validate()
  const payload: RegisterRequest = {
    username: registerForm.value.username,
    email: registerForm.value.email,
    phone: registerForm.value.phone,
    password: registerForm.value.password,
    nickname: registerForm.value.username,
  }
  const result = await authStore.register(payload)
  if (result.success) emit('success')
}
</script>

<style scoped>
.register-form {
  width: 100%;
  max-width: 400px;
}

.register-button {
  width: 100%;
  height: 48px;
  font-size: 1.1rem;
  background: linear-gradient(135deg, #ff5a00 0%, #ff0036 100%);
  border: none;
  border-radius: 0;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .register-form {
    max-width: 100%;
  }
  
  .register-button {
    height: 44px;
    font-size: 1rem;
  }
  
  .el-form-item {
    margin-bottom: 16px;
  }
}

/* 触摸设备优化 */
@media (hover: none) and (pointer: coarse) {
  .register-button {
    transition: all 0.2s ease;
  }
  
  .register-button:active {
    transform: scale(0.98);
  }
  
  .el-input {
    transition: all 0.2s ease;
  }
  
  .el-input:focus-within {
    transform: scale(1.02);
  }
}

/* 小屏幕优化 */
@media (max-width: 480px) {
  .register-button {
    height: 40px;
    font-size: 0.95rem;
  }
  
  .el-form-item {
    margin-bottom: 16px;
  }
}
</style>

